package com.dyz.leetcode._16_dataStructureDesign;

import java.util.*;

public class _380_RandomizedSet {
    private List<Integer> nums;
    private HashMap<Integer, Integer> map;
    private Random rand = new Random();

    public _380_RandomizedSet() {
        this.nums = new ArrayList<>();
        this.map = new HashMap<>();
    }

    public boolean insert(int val) {
        if(map.containsKey(val)){
            return false;
        }
        map.put(val, nums.size());
        this.nums.add(val);
        return true;
    }

    public boolean remove(int val) {
        if(!map.containsKey(val)){
            return false;
        }
        Integer index = map.get(val);
        int lastNum = nums.get(nums.size()-1);
        nums.set(index, lastNum);
        nums.remove(nums.size()-1);
        map.put(lastNum, index);
        map.remove(val);
        return true;
    }

    public int getRandom() {

        return nums.get(rand.nextInt(nums.size()));

    }
}
